Lesson 3 - Numbers in Python

Python 3.x has three number types: int, float, complex (version 2.x also had long for large integers)

  • Whole numbers: int
  • Decimal numbers: float
  • Imaginary numbers: complex

Floats have decimal points, integers don't.


In [1]:
a = 496 # This is a perfect number, and an integer
type(a) # verify type is int


Out[1]:
int

In [ ]:
a  # show contents of variable a

In [ ]:
print(a)  # another way to show contents of variable a

In [ ]:
e = 2.7118281828   # This one has a decimal, so its type is float
type(e)

Complex numbers $\Bbb{C}$ are expressed as a + bj in Python, not a + bi.


In [ ]:
z = 2 - 6.1j
type(z)

In [ ]:
z.real  # Get the real part a

In [ ]:
type(z.real)  # The real part is type float even though it was entered as 2

In [ ]:
z.imag  #  Get the imaginary part

In [ ]:
y = -1 + 3j ; y  # Python adds parens around complex numbers

In [ ]:
print(y + z)  # Shows some weirdness for floats

In [ ]: